package com.ruoyi.buy.service.impl;

import com.ruoyi.buy.domain.entity.PO.BuyProductItem;
import com.ruoyi.buy.domain.entity.PO.BuyPurchaseBill;
import com.ruoyi.buy.domain.entity.PO.BuyPurchaseBillItem;
import com.ruoyi.buy.domain.entity.buy0006.BuyProductItemImport;
import com.ruoyi.buy.domain.entity.importPO.BuyBatchPurchaseBillImport;
import com.ruoyi.buy.mapper.PO.BuyProductItemMapper;
import com.ruoyi.buy.mapper.PO.BuyPurchaseBillItemMapper;
import com.ruoyi.buy.mapper.PO.BuyPurchaseBillMapper;
import com.ruoyi.buy.service.IBuyProductItemService;
import com.ruoyi.buy.service.ICommonService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 投产登记Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-23
 */
@Service
@Transactional
public class BuyProductItemServiceImpl implements IBuyProductItemService
{
    @Resource
    private BuyProductItemMapper buyProductItemMapper;

    @Resource
    private ICommonService iCommonService;

    @Resource
    private BuyPurchaseBillMapper buyPurchaseBillMapper;

    @Resource
    private BuyPurchaseBillItemMapper buyPurchaseBillItemMapper;

    /**
     * 查询投产登记
     * 
     * @param iD 投产登记主键
     * @return 投产登记
     */
    @Override
    public BuyProductItem selectBuyProductItemByID(Long iD)
    {
        return buyProductItemMapper.selectBuyProductItemByID(iD);
    }

    /**
     * 查询投产登记列表
     * 
     * @param buyProductItem 投产登记
     * @return 投产登记
     */
    @Override
    public List<BuyProductItem> selectBuyProductItemList(BuyProductItem buyProductItem)
    {
        return buyProductItemMapper.selectBuyProductItemList(buyProductItem);
    }

    /**
     * 新增投产登记
     * 
     * @param buyProductItem 投产登记
     * @return 结果
     */
    @Override
    public int insertBuyProductItem(BuyProductItem buyProductItem)
    {
        return buyProductItemMapper.insertBuyProductItem(buyProductItem);
    }

    /**
     * 修改投产登记
     * 
     * @param buyProductItem 投产登记
     * @return 结果
     */
    @Override
    public int updateBuyProductItem(BuyProductItem buyProductItem)
    {
        return buyProductItemMapper.updateBuyProductItem(buyProductItem);
    }

    /**
     * 批量删除投产登记
     * 
     * @param iDs 需要删除的投产登记主键
     * @return 结果
     */
    @Override
    public int deleteBuyProductItemByIDs(Long[] iDs)
    {
        return buyProductItemMapper.deleteBuyProductItemByIDs(iDs);
    }

    /**
     * 删除投产登记信息
     * 
     * @param iD 投产登记主键
     * @return 结果
     */
    @Override
    public int deleteBuyProductItemByID(Long iD)
    {
        return buyProductItemMapper.deleteBuyProductItemByID(iD);
    }

    @Override
    public int batchSave(List<BuyProductItemImport> list) throws Exception {
        for (BuyProductItemImport item:list
             ) {
            BuyProductItem buyProductItem = new BuyProductItem();
            BeanUtils.copyProperties(item,buyProductItem);
            buyProductItem.setCreateBy(item.getCreateBy());
            buyProductItem.setCreateTime(DateUtils.getNowDate());
            buyProductItemMapper.insertBuyProductItem(buyProductItem);
        }

        Map<String, List<BuyProductItemImport>> map = list.stream().collect(Collectors.groupingBy(BuyProductItemImport::getvContractNo));
        for (Map.Entry<String, List<BuyProductItemImport>> entry : map.entrySet()) {
            if (!StringUtils.isEmpty(entry.getKey()) && !entry.getKey().equals("0")){
                dealContract(entry.getValue());
            }
        }

        return 1;
    }

    private void dealContract(List<BuyProductItemImport> list) throws Exception {
        //先处理合同主表信息
        BuyPurchaseBill buyPurchaseBill = new BuyPurchaseBill();

        BuyProductItemImport buyProductItemImport = list.get(0);
        //合同类型：机加
        buyPurchaseBill.setvType("01");
        //合同编号
        buyPurchaseBill.setvBillNo(buyProductItemImport.getvContractNo());
        //状态
        buyPurchaseBill.setStatus("0");
        //项目编号
        buyPurchaseBill.setvProjectNo(buyProductItemImport.getvProjectNo());
        //项目描述
        buyPurchaseBill.setvProjectDec(buyProductItemImport.getvProjectName());
        //删除标识
        buyPurchaseBill.setDelFlag("0");
        //申请人/设计
        buyPurchaseBill.setApplyBy(buyProductItemImport.getvDesigner());
        //经办人
        buyPurchaseBill.setOprBy(iCommonService.getUsername());
        //供应商/厂家
        buyPurchaseBill.setSupplier(buyProductItemImport.getSupplier());
        buyPurchaseBill.setCreateTime(DateUtils.getNowDate());
        buyPurchaseBill.setCreateBy(iCommonService.getUsername());
        buyPurchaseBill.setnDays(buyProductItemImport.getnDays());

        buyPurchaseBillMapper.insertBuyPurchaseBill(buyPurchaseBill);

        buyPurchaseBill = buyPurchaseBillMapper.selectBuyPurchaseBillList(buyPurchaseBill).get(0);

        //处理明细
        Long nMainId = buyPurchaseBill.getId();

        for (BuyProductItemImport item:list
        ) {
            //新增明细
            BuyPurchaseBillItem buyPurchaseBillItem = new BuyPurchaseBillItem();
            buyPurchaseBillItem.setnMainId(nMainId);
            buyPurchaseBillItem.setStatus("0");
            buyPurchaseBillItem.setCreateTime(DateUtils.getNowDate());
            buyPurchaseBillItem.setCreateBy(iCommonService.getUsername());
            //零件名称
            buyPurchaseBillItem.setvSpaName(item.getvSpaName());
            //规格型号/图纸号
            buyPurchaseBillItem.setvSpaModel(item.getvDrawingNo());
            //品牌/材料
            buyPurchaseBillItem.setvSpaBrand(item.getvMaterial());
            //数量
            if (this.isNumeric(item.getnQty())){
                buyPurchaseBillItem.setnQty(new BigDecimal(item.getnQty()));
            }
            //单价/合同单价
            buyPurchaseBillItem.setnPrice(item.getnContractPrice());
            //供应商/厂家
            buyPurchaseBillItem.setSupplier(item.getSupplier());
            buyPurchaseBillItem.setnDays(item.getnDays());
            buyPurchaseBillItem.setRemark(item.getRemark());
            this.buyPurchaseBillItemMapper.insertBuyPurchaseBillItem(buyPurchaseBillItem);

            this.iCommonService.changeSpaPrice(buyPurchaseBill,buyPurchaseBillItem,buyPurchaseBill.getCreateBy(),buyPurchaseBill.getvBillNo(),"BUY0003",buyPurchaseBill.getvType());

        }



    }

    private boolean isNumeric(String str){
        for(char ch : str.toCharArray()){
            if(!Character.isDigit(ch) && ch != '-' && ch != '.'){
                return false;
            }
        }
        return true;
    }
}
